package com.task.manager.mapper;

import org.apache.ibatis.jdbc.SQL;
import org.springframework.util.StringUtils;

import com.task.manager.domain.Task;

public class TaskSqlProvider {

    public String insertSelective(Task record) {
        SQL sql = new SQL();
        sql.INSERT_INTO("t_task");
        
        if (record.getId() != null) {
            sql.VALUES("ID", "#{id,jdbcType=INTEGER}");
        }
        
        if (record.getTaskName() != null) {
            sql.VALUES("TASK_NAME", "#{taskName,jdbcType=VARCHAR}");
        }
        
        if (record.getTaskCode() != null) {
            sql.VALUES("TASK_CODE", "#{taskCode,jdbcType=VARCHAR}");
        }
        
        if (record.getTaskHeader() != null) {
            sql.VALUES("TASK_HEADER", "#{taskHeader,jdbcType=VARCHAR}");
        }
        
        if (record.getTaskType() != null) {
            sql.VALUES("TASK_TYPE", "#{taskType,jdbcType=VARCHAR}");
        }
        
        if (record.getCallbackAddress() != null) {
            sql.VALUES("CALLBACK_ADDRESS", "#{callbackAddress,jdbcType=VARCHAR}");
        }
        
        if (record.getTaskParam() != null) {
            sql.VALUES("TASK_PARAM", "#{taskParam,jdbcType=VARCHAR}");
        }
        
        if (record.getTaskUrl() != null) {
            sql.VALUES("TASK_URL", "#{taskUrl,jdbcType=VARCHAR}");
        }
        
        if (record.getTaskCron() != null) {
            sql.VALUES("TASK_CRON", "#{taskCron,jdbcType=VARCHAR}");
        }
        
        if (record.getStatus() != null) {
            sql.VALUES("STATUS", "#{status,jdbcType=BIT}");
        }
        
        if (record.getRetryCount() != null) {
            sql.VALUES("RETRY_COUNT", "#{retryCount,jdbcType=INTEGER}");
        }
        
        if (record.getActuatorCode() != null) {
            sql.VALUES("ACTUATOR_CODE", "#{actuatorCode,jdbcType=VARCHAR}");
        }
        
        if (record.getCreateTime() != null) {
            sql.VALUES("CREATE_TIME", "#{createTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getUpdateTime() != null) {
            sql.VALUES("UPDATE_TIME", "#{updateTime,jdbcType=TIMESTAMP}");
        }
        
        return sql.toString();
    }

    public String updateByPrimaryKeySelective(Task record) {
        SQL sql = new SQL();
        sql.UPDATE("t_task");
        
        if (record.getTaskName() != null) {
            sql.SET("TASK_NAME = #{taskName,jdbcType=VARCHAR}");
        }
        
        if (record.getTaskCode() != null) {
            sql.SET("TASK_CODE = #{taskCode,jdbcType=VARCHAR}");
        }
        
        if (record.getTaskHeader() != null) {
            sql.SET("TASK_HEADER = #{taskHeader,jdbcType=VARCHAR}");
        }
        
        if (record.getTaskType() != null) {
            sql.SET("TASK_TYPE = #{taskType,jdbcType=VARCHAR}");
        }
        
        if (record.getCallbackAddress() != null) {
            sql.SET("CALLBACK_ADDRESS = #{callbackAddress,jdbcType=VARCHAR}");
        }
        
        if (record.getTaskParam() != null) {
            sql.SET("TASK_PARAM = #{taskParam,jdbcType=VARCHAR}");
        }
        
        if (record.getTaskUrl() != null) {
            sql.SET("TASK_URL = #{taskUrl,jdbcType=VARCHAR}");
        }
        
        if (record.getTaskCron() != null) {
            sql.SET("TASK_CRON = #{taskCron,jdbcType=VARCHAR}");
        }
        
        if (record.getStatus() != null) {
            sql.SET("STATUS = #{status,jdbcType=BIT}");
        }
        
        if (record.getRetryCount() != null) {
            sql.SET("RETRY_COUNT = #{retryCount,jdbcType=INTEGER}");
        }
        
        if (record.getActuatorCode() != null) {
            sql.SET("ACTUATOR_CODE = #{actuatorCode,jdbcType=VARCHAR}");
        }
        
        if (record.getCreateTime() != null) {
            sql.SET("CREATE_TIME = #{createTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getUpdateTime() != null) {
            sql.SET("UPDATE_TIME = #{updateTime,jdbcType=TIMESTAMP}");
        }
        
        sql.WHERE("ID = #{id,jdbcType=INTEGER}");
        
        return sql.toString();
    }
    
    public String queryTaskByParam(Task record) {
    	SQL sql = new SQL();
    	sql.SELECT("t.*");
    	sql.FROM("t_task t");
    	if(!StringUtils.isEmpty(record.getTaskCode())) {
    		sql.WHERE("t.task_code like CONCAT('%',#{taskCode,jdbcType=VARCHAR},'%') ");
    	}
    	
    	if(!StringUtils.isEmpty(record.getTaskName())) {
    		sql.WHERE("t.task_name like CONCAT('%',#{taskName,jdbcType=VARCHAR},'%') ");
    	}
    	sql.ORDER_BY("t.id desc");
    	return sql.toString();
    }
}